//
//  TimerView.m
//  Pods
//
//  Created by guoshiqing on 2017/8/9.
//
//

#import "TimerView.h"
#import "EPColor.h"
#import <NSObject+MemoryLeak.h>


@interface TimerView()

#pragma mark - 子视图
@property (nonatomic, strong) UILabel *lblTimeRemain;
@property (nonatomic, strong) UIButton *btnOpt;

#pragma mark - 控制变量
@property (nonatomic, assign) NSUInteger uiLeaveSecond;
@property (nonatomic, strong) NSTimer *timer;


@end

@implementation TimerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubVies];
    }
    return self;
}


// 防止内存泄露
- (BOOL)willDealloc {
    if (![super willDealloc]) {
        return NO;
    }
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    return YES;
}

#pragma mark - 添加子窗口
- (void)addSubVies
{
    self.lblTimeRemain = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:self.lblTimeRemain];
    self.lblTimeRemain.textAlignment = NSTextAlignmentLeft;
    [self.lblTimeRemain setFont:[UIFont systemFontOfSize:15.0f]];
    
    [self setLabelTransparency:TIMERVIEWSTATE_TRANSLUCENT];

    self.btnOpt = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:self.btnOpt];
    [self.btnOpt addTarget:self action:@selector(btnOptClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setLabelTransparency:(TIMERVIEWSTATE)state
{
//    CGFloat transparency = 1.0f;
    UIColor *color = [EPColor epColor_Blue];
    if (TIMERVIEWSTATE_TRANSLUCENT == state) {
//        transparency = 0.5;
        color = [EPColor epColor_Gray];
    }
//    [self.lblTimeRemain setTextColor:[color colorWithAlphaComponent:transparency]];
    self.lblTimeRemain.textColor = color;
}

- (void)setStartTitle:(NSString *)title
{
    _startTitle = title;
    self.lblTimeRemain.text = _startTitle;
}


#pragma mark - 点击按钮
- (void)btnOptClick:(id)sender
{
    if (self.blockOfClick) {
        self.blockOfClick();
    }
}

- (void)startTimer
{
    self.uiLeaveSecond = 60;
    if (!self.timer && ![self.timer isValid]){
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(resetTimeLeave) userInfo:nil repeats:YES];
        self.btnOpt.enabled = NO;
    }
}

- (void)resetBtn
{
    if (NO == self.btnOpt.enabled) {
        self.btnOpt.enabled = YES;
    }
    [self resetTimer];
}


#pragma mark - 设置定时器显示内容
- (void)resetTimeLeave
{
    // 设置定时按钮的显示
    self.uiLeaveSecond--;
    
    [self.lblTimeRemain setText:[NSString stringWithFormat:@"%lu%@", (unsigned long)self.uiLeaveSecond, @"s后，重发验证码"]];
    [self setLabelTransparency:TIMERVIEWSTATE_TRANSLUCENT];
    
    // 减为0时，停止定时器
    if (0 == self.uiLeaveSecond) {
        [self resetTimer];
    }
}

- (void)resetTimer
{
    [self setLabelTransparency:TIMERVIEWSTATE_FULLDISPLAY];
    self.lblTimeRemain.text = @"重发验证码";
    self.btnOpt.enabled = YES;
    
    // 释放定时器
    [self.timer invalidate];
    self.timer = nil;
}


#pragma mark - 当前是否可点击倒计时按钮
- (BOOL)canClick
{
    return self.btnOpt.enabled;
}

@end
